package com.tsinghuabigdata.edu.ddmath.adapter;

import android.content.Context;
import android.util.Log;

import com.squareup.picasso.Picasso;
import com.tsinghuabigdata.edu.ddmath.R;
import com.tsinghuabigdata.edu.ddmath.bean.LoginInfo;
import com.tsinghuabigdata.edu.ddmath.util.AccountUtils;
import com.tsinghuabigdata.edu.ddmath.util.AppLog;
import com.tsinghuabigdata.edu.ddmath.util.PicassoUtil;
import com.tsinghuabigdata.edu.ddmath.view.RoundImageView;

import java.io.File;
import java.net.URLEncoder;
import java.util.List;

/**
 * Created by Administrator on 2017/2/17.
 */

public class PreviewAdapter extends CommonAdapter<String> {

    private int    curPosition;
    private String accessToken;

    public PreviewAdapter(Context context, List<String> mDatas, int index) {
        super(context, mDatas);
        curPosition = index;
        LoginInfo loginInfo = AccountUtils.getLoginUser();
        if (loginInfo != null) {
            try {
                accessToken = URLEncoder.encode(loginInfo.getAccessToken(), "utf-8");
            } catch (Exception e) {
                AppLog.d(e.toString());
            }
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.item_picture_preview;
    }

    @Override
    protected void convert(ViewHolder helper, int position, String url) {
        helper.setText(R.id.tv_preview_position, String.valueOf(position + 1));
        RoundImageView imageView = helper.getView(R.id.iv_preview);

        if (url.startsWith("http")) {
            String comUrl = url;// + "?access_token=" + accessToken;
            Log.i("grubbyurl", "comUrl=" + comUrl);
            PicassoUtil.getPicasso(mContext).load(comUrl).error(R.drawable.ic_broken_image).placeholder(R.drawable.ic_temporary_image).into(imageView);
        } else {
            Picasso.with(mContext).load(new File(url)).error(R.drawable.ic_broken_image).placeholder(R.drawable.ic_temporary_image).into(imageView);
        }
//        LinearLayout linearLayout = helper.getView(R.id.ll_iv_preview);
//        if (position == curPosition) {
//            linearLayout.setBackgroundResource(R.drawable.bg_preview_select);
//        } else {
//            linearLayout.setBackgroundResource(R.drawable.bg_preview_unselect);
//        }
    }

    public void select(int position) {
        if (position != curPosition) {
            curPosition = position;
            notifyDataSetChanged();
        }
    }
}
